<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/Kid.php'); 
    require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/KidReport.php'); 
    require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/KidInOut.php'); 
    require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/Diapers.php'); 
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/Sleep.php'); 
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/Food.php'); 
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/Drink.php'); 
    require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/KidReport.php'); 
    require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/Temperature.php');
    require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/utils/CalendarUtils.php'); 
	
	require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/service/KidService.php'); 

	header("Content-Type:text/xml");  
	$kidService = new KidService();
	
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
	
	//dispatch kid is the major post file to handle save and load of kid creche info (add food, diper, read kid xml, ...)
	
	
	
	if($_POST['action'] == "readKidForCreche"){
        $kid = new Kid();
        $kid->setId($_POST['kidId']);
        
        
        $date = $_POST['date'];
        $dateoffset = $_POST['offset'];
        
        $datecalc = CalendarUtils::getOffsetFromDateKey($date, $dateoffset);
        
        
		echo $kidService->readKidInfoXml($kid, $datecalc);
    }else if($_POST['action'] == "readKidFamilyAndAdmin"){
        $kid = new Kid();
        $kid->setId($_POST['kidId']);
		echo $kidService->readKidFamilyAndAdmin($kid);
    
	}else if($_POST['action'] == "addDiaper"){
		$diaper = new Diapers();
		$diaper->setType($_POST['dType']);
        $diaper->setKidsId($_POST['kidId']);
		$diaper->setTime($_POST['dTimestamp']);
		echo $kidService->saveDiaperForKid($diaper)->toXml();
	}else if($_POST['action'] == "addSleepFrom"){
		$sleep = new Sleep();
		$sleep->setTimeFrom($_POST['sTimestamp']);
        $sleep->setKidsId($_POST['kidId']);
		echo $kidService->saveSleepForKid($sleep)->toXml();
	}else if ($_POST['action'] == "addSleepTo"){
		$sleep = new Sleep();
		$sleep->setTimeTo($_POST['sTimestamp']);
        $sleep->setKidsId($_POST['kidId']);
		echo $kidService->saveSleepForKid($sleep)->toXml();
        
    }else if($_POST['action'] == "addDrinkFrom"){
		$drink = new Drink();
		$drink->setTimeFrom($_POST['lTimestamp']);
		$drink->setDrinkTypesId($_POST['lType']);
		$drink->setKidsId($_POST['kidId']);
		echo $kidService->saveDrinkForKid($drink)->toXml();
	}else if($_POST['action'] == "addDrinkTo"){
		$drink = new Drink();
		$drink->setTimeTo($_POST['lTimestamp']);
		$drink->setAmount($_POST['lAmount']);
		$drink->setKidsId($_POST['kidId']);
		echo $kidService->saveDrinkForKid($drink)->toXml();
	}else if($_POST['action'] == "addFoodFrom"){
		$food = new Food();
		$food->setTimeFrom($_POST['fTimestamp']);
		$food->setRecipeId($_POST['fMenu']);
		$food->setKidsId($_POST['kidId']);
		echo $kidService->saveFoodForKid($food)->toXml();
	}else if($_POST['action'] == "addFoodTo"){
        $food = new Food();
		$food->setTimeTo($_POST['fTimestamp']);
		$food->setScore($_POST['fScore']);
        $food->setKidsId($_POST['kidId']);
		echo $kidService->saveFoodForKid($food)->toXml();
        
    }else if($_POST['action'] == "saveNote"){
        $kidReport = new KidReport();
        $kidReport->setKidsId($_POST['kidId']);
        $kidReport->setReport($_POST['notedata']);
        $kidReport->setTimestamp(new DateTime());
        echo $kidService->saveNote($kidReport)->toXml();
    }else if($_POST['action'] == "readNoteForKids"){
        $kidReport = new KidReport();
        $kidReport->setKidsId($_POST['kidId']);
        $kidReport->setTimestamp(new DateTime());
        
        echo $kidService->readNote($kidReport)->toXml();
    }else if($_POST['action'] == "saveTimeIn"){
        $kidInOut = new KidInOut();
        $kidInOut->setKidsId($_POST['kidId']);
        $kidInOut->setDate(CalendarUtils::getDbDateToday());
        $kidInOut->setInTime($_POST['inTime']);
        echo $kidService->saveInOut($kidInOut)->toXml();
    }else if($_POST['action'] == "saveTimeOut"){
        $kidInOut = new KidInOut();
        $kidInOut->setKidsId($_POST['kidId']);
        $kidInOut->setOutTime($_POST['outTime']);
        $kidInOut->setDate(CalendarUtils::getDbDateToday());
        echo $kidService->saveInOut($kidInOut)->toXml();
    }else if($_POST['action'] == "addTemperature"){
        $temperature = new Temperature();
        $temperature->setTime($_POST['tTimestamp']);
        $temperature->setDegree($_POST['tDegree']);
        $temperature->setKidsId($_POST['kidId']);
		echo $kidService->saveTemperature($temperature)->toXml();
		
    }
	

?>